﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using HYElectricSystemDiagram.DataStruct;
using HYElectricSystemDiagram.TMethod;
using HYView;

namespace HYElectricSystemDiagram
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdSystemDiagramPump : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			UIApplication application = commandData.Application;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Selection selection = application.ActiveUIDocument.Selection;
			if (SwitchView.SetUsableView(CmdViewType.FloorPlan, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			int num = 0;
			List<CurveLoop> userSelectedLoopsSystem = CurveloopsSelector.GetUserSelectedLoopsSystem(commandData, document, "普通潜污泵", out num);
			if (userSelectedLoopsSystem == null && num == -10)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			List<ElecMachPara> list = new List<ElecMachPara>();
			Dictionary<string, string> namSet = GetXmlData.GetNamSet();
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (KeyValuePair<string, string> keyValuePair in namSet)
			{
				if (keyValuePair.Value.Contains("普通潜污泵"))
				{
					dictionary.Add(keyValuePair.Key, keyValuePair.Value);
				}
			}
			if (dictionary.Count == 0)
			{
				YJKMessageBox.Information("未找到有效的类型名称，请在设置中名称设置进行添加！");
			}
			ElecMachPara curveAllElem;
			if (userSelectedLoopsSystem == null)
			{
				curveAllElem = GetElectricInfo.GetCurveAllElem(application, dictionary);
			}
			else
			{
				curveAllElem = GetElectricInfo.GetCurveAllElem(application, userSelectedLoopsSystem, dictionary);
			}
			list = GetElectricInfo.GetClassify(dictionary, curveAllElem);
			if (list.Count < 1)
			{
				YJKMessageBox.Information("未找到有效的控制箱，请设置正确的配电盘名称！");
				return Autodesk.Revit.UI.Result.Failed;
			}
			UserControlData1 userData = GetXmlData.GetUserData("普通潜污泵");
			userData.DicNameSet = dictionary;
			List<List<LineSettingIn>> lineSettingInInfo = GetXmlData.GetLineSettingInInfo(null);
			userData.m_LineSettingIn = lineSettingInInfo[userData.InCase];
			List<List<LineSettingOut>> lineSettingOutInfo = GetXmlData.GetLineSettingOutInfo(null);
			userData.m_LineSettingOut = lineSettingOutInfo[userData.OutCase];
			userData.m_WiringInstructions = GetXmlData.GetWiringInstructionsInfo(null);
			Result result;
			try
			{
				result = new SystemDiagramDrawingControler(commandData, list).DrawingSystemDiagramCmd(selection, userData);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
